<%
import urllib
import locale
from kontext.kontext import Kontext

locale.setlocale(locale.LC_ALL,"tr_TR.UTF-8")

class AppURLopener(urllib.FancyURLopener):
    def __init__(self, *args):
        self.version = "MSIE/5.0"
        apply(urllib.FancyURLopener.__init__, (self,) + args)

uri = AppURLopener()

def _s(val):
    if form.has_key(val):
        return form[val]
    return ''
# end of_

def content(url=None,q=None,options=None):
    if url:
        url = url.strip("'")
        page = uri.open(url).read()
        kontext = Kontext(page,q,options)
        return kontext.render()
    else:
        return "There is no url"
# end of content

%>

<html>
<head>
    <title>Kontext Summarize</title>
    <link rel="stylesheet" type="text/css" href="slider/slider.css">
    <script type="text/javascript">
        var temp,temp2 = new Array();
        var givenOptions = "<%=_s('options')%>"
        options = new Array();

        temp = givenOptions.split(',');
        for (var i=0;i<temp.length;i++) {
            temp2 = temp[i].split(':');
            options[temp2[0]]= temp2[1];
        }
        
        function init(){
            uriEl = document.getElementById('uri');
            wordsEl = document.getElementById('words');
            methodEl     = document.getElementById('method');
            formatEl     = document.getElementById('format');
            sortedEl     = document.getElementById('sorted');
            markEl       = document.getElementById('mark');
            debugEl      = document.getElementById('debug');
            showScoresEl = document.getElementById('showScores');
            sortedEl.checked=true;
            markEl.checked=true;
            for (x in options) {
                if (x=="method"){
                    if (options[x] == "M1")
                        methodEl.selectedIndex = 0;
                    if (options[x] == "M2")
                        methodEl.selectedIndex = 1;
                    if (options[x] == "M3")
                        methodEl.selectedIndex = 2;
                }
                if (x=="format") {
                    if (options[x] == "html")
                        formatEl.selectedIndex = 0;
                    else
                        formatEl.selectedIndex = 1;
                }
                if (x=="sorted")
                    if (options[x]=="true")
                        sortedEl.checked=true;
                    else
                        sortedEl.checked=false;
                if (x=="mark")
                    if (options[x]=="true")
                        markEl.checked=true;
                    else
                        markEl.checked=false;
                if (x=="debug")
                    if (options[x]=="true")
                        debugEl.checked=true;
                    else
                        debugEl.checked=false;
                if (x=="showScores")
                    if (options[x]=="true")
                        showScoresEl.checked=true;
                    else
                        showScoresEl.checked=false;
            }
        }
    
        
        function tV(el, src) {
            var v = el.style.display == "block";
            var str = src.innerHTML;
            el.style.display = v ? "none" : "block";
            src.innerHTML = v ? str.replace(/hide/, "show") : str.replace(/show/, "hide");
        }
    </script>
    
    <script type="text/javascript" src="slider/slider.js"></script>
    <script type="text/javascript" src="slider/kontext.js"></script>
    <script>
        function updateSum() {
            if (methodEl.selectedIndex==0)
                meth = 'M1';
            else if (methodEl.selectedIndex==1)
                meth = 'M2';
            else 
                meth = 'M3';
            
            if (formatEl.selectedIndex==0)
                formet = 'html';
            else
                formet = 'plain';
                
            dest = "?url="+uriEl.value+"&q="+wordsEl.value+
                   "&options=sorted:"+sortedEl.checked+",mark:"+markEl.checked+
                   ",debug:"+debugEl.checked+",showScores:"+showScoresEl.checked+",limit:"+valueAsInt+",method:"+meth+",format:"+formet;
            location.href = dest;
        }
    </script>
    <style>
        #debug,#pageInfo {background-color:#FFF6BD;}
        .hi {background-color:#FFF6BD;}
    </style>
</head>
<body>
<%
if not form.has_key('modal'):
    #
%>
    <div id="container">
        <div id="top">
            <h1>Kontext - Automatic Text Summarization</h1>
        </div>
        <form name="mainForm" action="">
        <div id="leftnav">
            <span id="result">Length of Summary : </span><br> <input type="text" value="75" name="speed" id="speed" class="slider" />
        </div>
        <div id="content">
            <table>
                <tr>
                    <td>Page Url : </td>
                    <td><input type="text" name="uri" id="uri" size=40 value="<%=_s('url')%>"></td>
                    <td>Method : </td>
                    <td>
                        <select name="methodoloji" id="method">
                            <option value="M1">HTML Structure Based
                            <option value="M2">Content Based
                            <option value="M3">Content Based - Random
                        </select>
                    </td>
                    <td>Format : </td>
                    <td>
                        <select name="formatt" id="format">
                            <option value="html">HTML Based
                            <option value="plain">Text Based
                        </select>
                    </td>
                    <td rNull owspan="2">
                        <input type="button" value="update" onClick="updateSum();">
                    </td>
                </tr>
                <tr>
                    <td>Search String : </td>
                    <td><input type="text" name="words" id="words" size=40 value="<%=_s('q')%>"></td>
                    <td>Options : </td>
                    <td colspan="3">
                        <input type="checkbox" id="showScores">Show Scores
                        <input type="checkbox" id="sorted">Sorted
                        <input type="checkbox" id="mark">Mark Strings
                        <input type="checkbox" id="debug">Debug On
                    </td>
                </tr>
            </table>
        </div>
        </form>
    </div>
<%
#
%>
<%= psp.apply_data(content) %>
</body>
</html>
